/*******************************************************************************
 * Version: 1.0.0
 *
 * \file   main.c
 * \brief  This application demonstrate basing functionality
 *
 *******************************************************************************
 * <b> (C) Copyright 2016 Silicon Labs, http://www.silabs.com </b>
 *******************************************************************************
 * This file is licensed under the Silicon Labs License Agreement. See the file
 * "Silabs_License_Agreement.txt" for details. Before using this software for
 * any purpose, you must agree to the terms of that agreement.
 *
 ******************************************************************************/

/*******************************************************************************
 *******************************   INCLUDES   **********************************
 ******************************************************************************/
#include <stdint.h>
#include <string.h>
#include <stdio.h>
#include "em_device.h"
#include "native_gecko.h"
#include "gecko_configuration.h"
#include "gatt_db.h"

#include "InitDevice.h"
#include "bg_dfu.h"
#include "aat.h"

/*******************************************************************************
 *****************************   LOCAL DATA   **********************************
 ******************************************************************************/
 #define MAX_CONNECTIONS 1
uint8_t bluetooth_stack_heap[DEFAULT_BLUETOOTH_HEAP(MAX_CONNECTIONS)];

/* Gecko configuration parameters (see gecko_configuration.h) */
static const gecko_configuration_t config = {
  .config_flags=0,
  .sleep.flags=SLEEP_FLAGS_DEEP_SLEEP_ENABLE,
  .bluetooth.max_connections=MAX_CONNECTIONS,
  .bluetooth.heap=bluetooth_stack_heap,
  .bluetooth.heap_size=sizeof(bluetooth_stack_heap),
  .gattdb=&bg_gattdb_data,
};

/*******************************************************************************
 *****************************   LOCAL FUNCTION   ******************************
 ******************************************************************************/
/*******************************************************************************
 * @brief
 *   Hardware initialization
 * @param
 *   None
 * @return
 *   None
 ******************************************************************************/

#include "em_gpio.h"

// LED pin defines for BGM111 development kit, BRD4300A.
// check the LED pins if you are using some other radio board.
#define LED0_bank		gpioPortF
#define LED0_pin      	6

#define LED1_bank		gpioPortF
#define LED1_pin      	7

#define TIMER_LED_BLINK 1

static void hardware_init(void)
{

	// configure LED pins as outputs

	GPIO_PinModeSet(LED0_bank, LED0_pin, gpioModePushPull, 1 );
	GPIO_PinModeSet(LED1_bank, LED1_pin, gpioModePushPull, 1 );

	// initial state: both LEDs off
	GPIO_PinOutSet(LED0_bank, LED0_pin);
	GPIO_PinOutSet(LED1_bank, LED1_pin);

}

/*******************************************************************************
 * @brief
 *   BLE service initialization
 * @param
 *   None
 * @return
 *   None
 ******************************************************************************/
static void services_init(void)
{
}

/*******************************************************************************
 * @brief
 *   LED blink, called from soft timer
 * @param
 *   None
 * @return
 *   None
 ******************************************************************************/
static void ledBlink(void)
{
	static int toggle = 0;

	if(toggle == 0)
	{
		GPIO_PinOutSet(LED0_bank, LED0_pin);
		GPIO_PinOutClear(LED1_bank, LED1_pin);
		toggle = 1;
	}
	else
	{
		GPIO_PinOutClear(LED0_bank, LED0_pin);
		GPIO_PinOutSet(LED1_bank, LED1_pin);
		toggle = 0;
	}

}

/*******************************************************************************
 * @brief
 *   Main()
 * @param
 *   None
 * @return
 *   None
 ******************************************************************************/
void main()
{
  struct gecko_cmd_packet *evt;

  // Initialize Blue Gecko module
  enter_DefaultMode_from_RESET();
  gecko_init(&config);

  // Hardware initialization
  //hardware_init();

  // Services initialization
  services_init();

  while (1) {
    /* Event pointer for handling events */
    struct gecko_cmd_packet* evt;

    /* Check for stack event. */
    evt = gecko_wait_event();

    /* Handle events */
    switch (BGLIB_MSG_ID(evt->header)) {

      /* This boot event is generated when the system boots up after reset.
       * Here the system is set to start advertising immediately after boot procedure. */
      case gecko_evt_system_boot_id:

		  hardware_init();
        /* Set advertising parameters. 100ms advertisement interval. All channels used.
         * The first two parameters are minimum and maximum advertising interval, both in
         * units of (milliseconds * 1.6). The third parameter '7' sets advertising on all channels. */
        gecko_cmd_le_gap_set_adv_parameters(160,160,7);

        /* Start general advertising and enable connections. */
        gecko_cmd_le_gap_set_mode(le_gap_general_discoverable, le_gap_undirected_connectable);

        /* start 500ms timer for LED blinking */
        gecko_cmd_hardware_set_soft_timer(32768/2,TIMER_LED_BLINK,0);

        break;

      case gecko_evt_le_connection_closed_id:
        /* Restart advertising after client has disconnected */
        gecko_cmd_le_gap_set_mode(le_gap_general_discoverable, le_gap_undirected_connectable);
        break;

      case gecko_evt_hardware_soft_timer_id:

    	  switch (evt->data.evt_hardware_soft_timer.handle) {

    	     case TIMER_LED_BLINK:
    	        ledBlink();
    	        break;
    	     default:
    	    	 break;
    	  }

    	  break;

      default:
        break;
    }
  }
}
